//
//  GDMainViewController.m
//  美团试验 (自己)
//
//  Created by 热带雨林 on 17/2/12.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDMainViewController.h"
#import "Masonry.h"
#import "GDThreeButtonView.h"
#import "CZAdditions.h"
#import "GDThreeButtonView.h"

#define topHeight 123

@interface GDMainViewController ()<UIScrollViewDelegate,GDThreeButtonDelegate,UIGestureRecognizerDelegate>

@property(nonatomic, weak)UIView  *topView;
@property(nonatomic, weak)GDThreeButtonView *threeBtnView;
@property(nonatomic, weak)UIScrollView *scrollView;

@end

@implementation GDMainViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
//    self.navigationController.navigationBar.alpha = 0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"美食";
    self.view.backgroundColor = [UIColor whiteColor];
}
//解决手势冲突的关键
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;
    
}
- (void)setupUI{
    UIView *topView = [[UIView alloc]init];
    topView.backgroundColor = [UIColor redColor];
    self.topView = topView;
    [self.view addSubview:topView];
    //在topView中放张图
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:@"tmpdir__17_2_17_22_56_57"];
    [self.view addSubview:imageView];
    GDThreeButtonView *threeBtnView = [[GDThreeButtonView alloc]init];
    self.threeBtnView = threeBtnView;
    self.threeBtnView.delegate = self;
    [self.view addSubview:threeBtnView];
    UIScrollView  *scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor = [UIColor greenColor];
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.delegate = self;
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(topHeight);
    }];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.width.right.equalTo(topView);
    }];
    [threeBtnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(37);
        make.top.equalTo(topView.mas_bottom);
    }];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(threeBtnView.mas_bottom);
        make.bottom.equalTo(self.view);
    }];
    
    //添加一个拖拽手势在屏幕上，确定顶部视图的高度
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];
    //根据名称添加三个控制器
    //注意：scrollView需要的是与 PageController一起使用
    NSArray <NSString *>*threeVC = @[@"GDBusinessViewController",@"GDTejiaViewController",@"GDAihaoViewController"];
    //注意：需要的不是控制器，而是控制器的视图
    NSMutableArray <UIView *> *threeVM = [NSMutableArray array];
    [threeVC enumerateObjectsUsingBlock:^(NSString * _Nonnull VCname, NSUInteger idx, BOOL * _Nonnull stop) {
        Class cla = NSClassFromString(VCname);
        GDBaseViewController *thVC = [[cla alloc]init];
        //将创建的控制器，作为子控制器添加到scrollView中
        [self cz_addChildController:thVC intoView:scrollView];
//        添加子控制器 － 否则响应者链条会被打断，导致事件无法正常传递，而且错误非常难改！ 不能拆开了写
//        [self addChildViewController:thVC];
//        [self.view addSubview:thVC.view];
//        [thVC didMoveToParentViewController:self];
        [threeVM addObject:thVC.view];
    }];
    //对三个子控制器的视图进行布局
    [threeVM mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [threeVM mas_makeConstraints:^(MASConstraintMaker *make) {
       //设置约束时，只是设置顶部与底部的约束
        make.top.bottom.equalTo(scrollView);
        make.size.equalTo(scrollView);
    }];
}
- (void)pan:(UIPanGestureRecognizer *)sender{
    
    //手势的添加
    CGPoint offset = [sender translationInView:sender.view];
    //细节优化
    if(ABS(offset.x) > ABS(offset.y)){
        return ;
    }
    
    [sender setTranslation:CGPointZero inView:sender.view];
    
    CGFloat height = self.topView.bounds.size.height + offset.y;
    
    //需要在64 － 123 之间来回滚动
    if(height < 64 || height > topHeight){
        return ;
    }
    
    //更新约束
    [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
    
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];
    
    //滚动的同时alpha在改变
    //注意：是 1.0（有小数，有缓和的余地） 不是 1
    CGFloat scale = 1.0/(topHeight - 64);
    CGFloat alpha = 1 - (height - 64)*scale;
    
    self.navigationController.navigationBar.alpha = alpha;
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGPoint offset = scrollView.contentOffset;
    
    //此方法是消除小黄条有弹簧效果的解决办法
    if(!(scrollView.tracking || scrollView.dragging ||scrollView.decelerating)){
        return ;
    }
    
    self.threeBtnView.offset = offset.x / 3;
    
}
- (void)threeButtonView:(GDThreeButtonView *)threBV withIndex:(NSInteger)index{
    
    CGRect rect = CGRectMake(index * _scrollView.bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, _scrollView.bounds.size.height);

    [self.scrollView scrollRectToVisible:rect animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
